/**
 * Leetcode 203. Remove Linked List Elements
 * https://leetcode.com/problems/remove-linked-list-elements/description/
 * <p>
 * 删除列表中的元素
 *
 * @author Kevin
 * @date 2018-11-26
 */
public class Solution6 {

    public static void main(String[] args) {

        int[] nums = {1, 2, 6, 3, 4, 5, 6};
        ListNode head = new ListNode(nums);
        System.out.println(head);

        ListNode res = (new Solution6()).removeElements(head, 6, 0);
        System.out.println(res);
    }

    /**
     * 根据值删除元素
     *
     * @param head
     * @param val
     * @param depth
     * @return
     */
    public ListNode removeElements(ListNode head, int val, int depth) {
        String depthStr = generateDepthStr(depth);
        System.out.println(depthStr);
        System.out.println("Call: remove " + val + " in " + head);


        if (head == null) {
            System.out.println(depthStr);
            System.out.println("Return: " + head);
            return head;
        }

        ListNode res = removeElements(head.next, val, depth + 1);
        System.out.println(depthStr);
        System.out.println("After remove " + val + " : " + res);

        ListNode ret;
        if (head.val == val) {
            ret = res;
        } else {
            head.next = res;
            ret = head;
        }
        System.out.println(depthStr);
        System.out.println("Return: " + ret);

        return ret;
    }

    private String generateDepthStr(int depth) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            sb.append("--");
        }

        return sb.toString();
    }

}
